//
//  WCNOrderDetailVC.m
//  WCN
//
//  Created by 杨鑫 on 15/7/8.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNOrderDetailVC.h"
#import "WCNServiceRecordVC.h"
#import "WCNCommentVC.h"
#import "WCNServiceTimeVC.h"
#import "WCNPatientAddVC.h"
#import "WCNCareRecordVC.h"
//
#import "WCButton.h"
#import "MJRefresh.h"
#import "WCNOrderDetailInfoTCell.h"
#import "WCNOrderDetailRemarkTCell.h"
#import "WCNOrderDetailPatientInfoTCell.h"
#import "WCNSysDetailTCell.h"
//
#import "UINavigationItem+Offset.h"
#import "UILabel+Utilities.h"
//
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
#import "WCCoreDataStore.h"
#import "WCDefaultManager.h"
//
#import "Macros.h"
#import "Constants.h"
#import "WCPicsCell.h"
#import "WeekDayModel.h"
#import "WCNMeSetTimes(replace)VC.h"
#import "WCChooseTimeVC.h"
#import "WCDayModel.h"
#import "WCDateMdoel.h"
#import "NSString+Utilities.h"
#import "WCNOrderDetailFooterView.h"
#import "Order.h"
#import "UMMobClick/MobClick.h"

@interface WCNOrderDetailVC ()<UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate>
{
    WCPicsCell *_cell;
    NSArray *_imageArray;
    NSInteger _nurseId;
    NSInteger _payMode;  // 1 线上支付  2 线下支付   3 取消订单
    float  _amountPaid;
    NSInteger _temp;
    
 
}
@property (weak, nonatomic) UITableView *tableView;
@property (strong, nonatomic) WCNOrderDetailPatientInfoTCell *patientInfoCell;
@property (strong, nonatomic) WCNOrderDetailRemarkTCell *remarkCell;
@property (weak, nonatomic) UIButton *footerButton;

@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) Order *order;
@property (strong, nonatomic) NSArray *serviceArray;
@property (strong, nonatomic) OrderComment *comment;
@property (strong, nonatomic) Patient *patient;

@end

@implementation WCNOrderDetailVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _order = [[Order alloc] init];
        _temp = 0;
        _url = @"nursing/order/detail";
        Account *accout =[[WCDefaultManager shareMg]account];
        _nurseId =[accout.stringId integerValue];
        _payMode = 1;
//        _amountPaid = [_order.amountPaid floatValue];//默认 实际支付金额为订单的金额
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOrderDetail) name:OrderDetailRefreshNotification object:nil];
    }
    return self;
}

-(void)loadView
{
    [super loadView];

    self.title = @"订单详情";
//    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypePatient];
//    [rightBtn addTarget:self action:@selector(addPatient) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationItem addRightBarButtonItem:rightBtn];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-60) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.backgroundColor = kAppBg;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    
//    [self loadTableFooterView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refresh];
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNOrderDetailPatientInfoTCell" bundle:nil] forCellReuseIdentifier:@"WCNOrderDetailPatientInfoTCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNOrderDetailRemarkTCell" bundle:nil] forCellReuseIdentifier:@"WCNOrderDetailRemarkTCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNOrderDetailInfoTCell" bundle:nil] forCellReuseIdentifier:@"WCNOrderDetailInfoTCell"];
    [self.tableView registerClass:[WCNSysDetailTCell class] forCellReuseIdentifier:@"WCNSysDetailTCell"];
    [self.tableView registerClass:[WCPicsCell class] forCellReuseIdentifier:myCellId];
    self.patientInfoCell = [self.tableView dequeueReusableCellWithIdentifier:@"WCNOrderDetailPatientInfoTCell"];
    self.patientInfoCell.address.numberOfLines = 0;
    [self.patientInfoCell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.patientInfoCell.contentView
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:nil
                                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                                multiplier:1
                                                                                  constant:kScreenWidth]];
    
    self.remarkCell = [self.tableView dequeueReusableCellWithIdentifier:@"WCNOrderDetailRemarkTCell"];
    self.remarkCell.remark.numberOfLines = 0;
    [self.remarkCell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.remarkCell.contentView
                                                                            attribute:NSLayoutAttributeWidth
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:nil
                                                                            attribute:NSLayoutAttributeNotAnAttribute
                                                                           multiplier:1
                                                                             constant:kScreenWidth]];
    
    [self.tableView.header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[WCNetworkAccessManager sharedMg] timeSinceLastUrl:self.url] > kUrlCD && ![self.tableView.header isRefreshing]) {
        [self.tableView.header beginRefreshing];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Others

- (void)loadTableFooterView
{

    //见面咨询  订单 状态为 待确认状态 (唯一的情况)
    ServiceType *serviceType = self.order.service_Type;
    NursingOrderStatus *orderStatu =self.order.nursingOrder_Status;
    if ([_order.orderSource integerValue] == 1 && [orderStatu.property isEqualToString:@"APPLYED"]) {
        
        UIView *footerView = [UIView new];
        [footerView setFrame:CGRectMake(0, 0, kScreenWidth, 100)];
        NSArray *confirTitileArr =@[@"确认订单",@"取消订单"];
        CGFloat confirmBtnW = 100;
        CGFloat confirmBtnH = 30;
        CGFloat edgeS =(kScreenWidth - 60 -confirmBtnW *2)/2;
        for (int i  = 0; i< 2; i++) {
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake(edgeS + (confirmBtnW +60)*i, (footerView.frame.size.height - confirmBtnH)/2, confirmBtnW, confirmBtnH);
            btn.backgroundColor  = HexRGB_WSJ(22, 155, 244);
            
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitle:confirTitileArr[i] forState:UIControlStateNormal];
            btn.tag = 300 + i;
            btn.layer.cornerRadius =10;
            btn.layer.masksToBounds =YES;
            [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font  =[UIFont systemFontOfSize:20];
            [footerView addSubview:btn];
        }
        self.tableView.tableFooterView = footerView;
        return;
    }
    if ([serviceType.serviceMode integerValue] ==1 && [orderStatu.property isEqualToString:@"APPLYED"]) { //已提交 未确认
        weakSelf_define;
        WCNOrderDetailFooterView *footerView =[[WCNOrderDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        footerView.showEditorBlock =^(){
            return weakSelf.view;
        };
        footerView.choosePayMethod = ^(BOOL isOnlie){
            ;
            if (isOnlie) {
                _payMode = 1;
            }else{
                _payMode = 2;
            }
            _temp = 0;
            Log(@"%d",_payMode);
        };
        footerView.priceChangedBlock = ^(float price){
            _amountPaid = price;
        };
        footerView.confimOrderBlock =^(BOOL isComfirm){
            
            [weakSelf comfirmUncomfirmOrder:isComfirm];
        };
        self.tableView.tableFooterView = footerView;
        return;
    }
    
    if (self.footerButton) {
        [self.footerButton removeTarget:self action:@selector(choiceNextServiceTime) forControlEvents:UIControlEventTouchUpInside];
        [self.footerButton removeTarget:self action:@selector(overOrder) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //剩下次数>0 且状为为非取消状态  则可配置下次服务

    if ([self.order.apptRemaining integerValue]> 0 &&![orderStatu.property isEqualToString:@"CANCELED"] ) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 66)];
        self.tableView.tableFooterView = footerView;
        
        UIButton *orderButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 0, kScreenWidth-50, 40)];
        self.footerButton = orderButton;
        orderButton.backgroundColor = HexRGB(0x00abf3);
        orderButton.layer.cornerRadius = 4;
        [orderButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [orderButton setTitle:@"下次服务预约" forState:UIControlStateNormal];
        [orderButton addTarget:self action:@selector(choiceNextServiceTime) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:orderButton];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 26)];
        label.text = @"完成本次服务后，请帮用户预约下次服务时间!";
        label.textColor = HexRGB(0x61606f);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12.0];
        [footerView addSubview:label];
    } else if ([orderStatu.property isEqualToString:@"PAID"]) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 55)];
        self.tableView.tableFooterView = footerView;
        
        UIButton *orderButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 0, kScreenWidth-50, 40)];
        self.footerButton = orderButton;
        orderButton.backgroundColor = HexRGB(0x00abf3);
        orderButton.layer.cornerRadius = 4;
        [orderButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [orderButton setTitle:@"完成订单" forState:UIControlStateNormal];
        [orderButton addTarget:self action:@selector(overOrder) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:orderButton];
    } else {
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
}
#pragma -mark  提交待确认订单

- (void)btnClicked:(UIButton *)btn {


        if (btn.tag - 300 ==0) {
            [self comfirmUncomfirmOrder:YES];
        }else{
            [self comfirmUncomfirmOrder:NO];
        }
}

- (void)comfirmUncomfirmOrder:(BOOL)isComfirm
{
   
    NSString *title;
    if (isComfirm) {
    title  = @"是否确认订单";
        if (_temp!= 0) {
            
            _payMode = _temp;
        }
    }else{  //取消订单
     title = @"是否取消订单";
        _temp = _payMode;
        _payMode = -1;
    
    }
    
    Log(@"%d",_payMode);
     UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提示" message:title delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alert show];

}
- (void)alertViewCancel:(UIAlertView *)alertView
{
    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex ==0) {
        return;
    }
    if ([_order.orderSource integerValue] == 1) {
        _payMode = 2;
    }
    NSMutableDictionary *parameter =[NSMutableDictionary dictionary];
    parameter[@"orderId"] =  self.orderId ;
    parameter[@"payMode"] =@(_payMode);
    parameter[@"amountPaid"] =@(_amountPaid);
    NSString *url;
             // 取消订单接口
        // 提交订单接口
    url =kComfirmOrder_part;
    MBProgressHUD *hud = showHud;
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] POST:url parameters:parameter success:^(NSDictionary *responseObject) {
        BOOL isSuccess = (BOOL)responseObject[@"success"];
        if (isSuccess) {
//          统计取消订单
            if (_payMode==-1) {
            [MobClick event:CancelOrder];
                  //统计确认订单
            }else{
             [MobClick event:ConfirmOrder];
            }
           
          [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];
    
    
}
- (void)refreshOrderDetail
{
    self.serviceArray = nil;
    
    if (![self.tableView.header isRefreshing]) {
        [self.tableView.header beginRefreshing];
    }
}
- (NSArray *)saveImageIds:(NSDictionary *)dataDict
{
    NSMutableArray *array =[NSMutableArray array];
    if (dataDict[@"image1"]) {
        [array addObject:dataDict[@"image1"]];
    }
    if(dataDict[@"image2"]){
        [array addObject:dataDict[@"image2"]];
    }
    if (dataDict[@"image3"]) {
        [array addObject:dataDict[@"image3"]];
    }
    
    return array;
    
}
- (void)refresh
{
    NSDictionary *params =@{@"orderId":self.orderId};
    [[WCNetworkAccessManager sharedMg] GET:_url parameters:params success:^(NSDictionary *result){
      
            NSDictionary *orderDict =result[@"data"];
        
            _order.nursingOrder_Status =[[NursingOrderStatus alloc]init];
            [_order.nursingOrder_Status setValuesForKeysWithDictionary:orderDict[@"nursingOrderStatus"]];
            
            
            _order.service_Type =[[ServiceType alloc] init];
            [_order.service_Type setValuesForKeysWithDictionary:orderDict[@"serviceType"]];
            
            [_order setValuesForKeysWithDictionary:orderDict];
        
       

         _imageArray = [self saveImageIds:result[@"data"]];
        [self.tableView reloadData];
        [self loadTableFooterView];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

//  舍弃掉
//- (void)addPatient
//{
//    if (self.patient) {
//        WCNCareRecordVC *vc = [[WCNCareRecordVC alloc] initWithTitle:self.patient mode:WCNCareRecordVCModeEdit];
//        [self.navigationController pushViewController:vc animated:YES];
//    } else {
//        MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
//        NSString *url = [NSString stringWithFormat:@"nursing/record/detail/by/%@", self.order.orderId];
//        [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
//            Patient *patient = [[WCCoreDataStore shareStore] createPatient:nil save:YES];
//            #warning Illegal attempt to establish a relationship 'mainNurse' between objects in different contexts
//            //在上面的Patient的save之前没传YES，导致没有放入上下文中，更新时报错
//            [[WCCoreDataStore shareStore] updateIdPatient:patient withData:result[@"data"]];
//            self.patient = patient;
//            
//            WCNCareRecordVC *vc = [[WCNCareRecordVC alloc] initWithTitle:self.patient mode:WCNCareRecordVCModeEdit];
//            [self.navigationController pushViewController:vc animated:YES];
//        } failure:^(NSString *msg){
//            WCNPatientAddVC *vc = [[WCNPatientAddVC alloc] initWithOrder:self.order.orderId];
//            [self.navigationController pushViewController:vc animated:YES];
//        } netError:nil always:^{
//            [hud hide:YES];
//        }];
//    }
//}
//
- (void)choiceNextServiceTime
{
    weakSelf_define;
   
    MBProgressHUD *hud = showHud;
    [[WCNetworkAccessManager sharedMg]  POST:kAvailableTimes parameters:@{@"nurseId":@(_nurseId)} success:^(NSDictionary *responseObject) {
        NSMutableArray *array =[NSMutableArray array];
        for (NSDictionary *dict in responseObject[@"data"]) {
            WCDateMdoel *date =[[WCDateMdoel alloc]init];
            [date setValuesForKeysWithDictionary:dict];
            [array addObject:date];
        }
        WCChooseTimeVC *vc =[[WCChooseTimeVC alloc]init];
        vc.dataArray =array;
        vc.callBack =^(NSString *timePeriodId,NSString *date,NSString *timePeriod){
            [weakSelf updateNextServiceTimeWithTargetDate:date timePerioId:[timePeriodId integerValue]];
        };
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } failure:^(NSString *msg) {
        
    } netError:nil always:^{
        [hud hide:YES];
    }];
    
    
//    WCNServiceTimeVC *vc = [[WCNServiceTimeVC alloc] initWithOrderId:self.order.stringId];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)updateNextServiceTimeWithTargetDate:(NSString *)date timePerioId:(NSInteger)timePerioId
{
//    POST /care-nurse/nursing/order/appointment/next
//    {
//        "orderId": 1,
//        "targetDate": "2015-05-13",
//        "timePeriodId": 20
//    }
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"orderId"] =_order.orderId;
    params[@"targetDate"] =date;
    params[@"timePeriodId"] =@(timePerioId);
    MBProgressHUD *hud =[[WCPopupLayerManager shareMg] showWithLabel:@"设置中.." withView:self.view];
    [[WCNetworkAccessManager sharedMg]POST:kAppointNextTime parameters:params success:^(NSDictionary *responseObject) {
       [[WCPopupLayerManager shareMg] showTextOnly:@"下次预约时间设置成功" withView:self.view];
        
        
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];
    
    
    
 
}
- (void)overOrder
{
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
    NSString *url = [NSString stringWithFormat:@"nursing/order/complete/%@", self.order.orderId];
    defineWeakSelf;
    [[WCNetworkAccessManager sharedMg] POST:url parameters:nil success:^(NSDictionary *result){
        
        //统计完成订单
        [MobClick event:CompeleteOrder];
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:OrderDetailRefreshNotification object:nil userInfo:@{@"tab":@2}];
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 3) {
        if (self.order.commentId) {
            OrderComment *comment = [[WCCoreDataStore shareStore] getOrderCommentById:self.order.commentId];
            if (comment) {
                WCNCommentVC *vc = [[WCNCommentVC alloc] init];
                vc.comment = comment;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
                NSString *url = [NSString stringWithFormat:@"nursing/order/comment/detail/%@", self.order.commentId];
                [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
                    NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:result[@"data"]];
                    data[@"id"] = self.order.commentId;
                    WCNCommentVC *vc = [[WCNCommentVC alloc] init];
                    vc.comment = [[WCCoreDataStore shareStore] createOrderComment:data save:YES];
                    [self.navigationController pushViewController:vc animated:YES];
                } failure:nil netError:nil always:^{
                    [hud hide:YES];
                }];
            }
        } else {
            if (self.comment) {
                WCNCommentVC *vc = [[WCNCommentVC alloc] init];
                vc.comment = self.comment;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
                NSString *url = [NSString stringWithFormat:@"nursing/order/comment/detail/by/%@", self.order.orderId];
                [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
                    self.comment = [[WCCoreDataStore shareStore] createOrderComment:result[@"data"] save:NO];
                    
                    WCNCommentVC *vc = [[WCNCommentVC alloc] init];
                    vc.comment = self.comment;
                    [self.navigationController pushViewController:vc animated:YES];
                } failure:nil netError:nil always:^{
                    [hud hide:YES];
                }];
            }
        }

//        if (indexPath.row == 0) {
//            if (self.serviceArray) {
//                WCNServiceRecordVC *vc = [[WCNServiceRecordVC alloc] initWithArray:self.serviceArray];
//                [self.navigationController pushViewController:vc animated:YES];
//            } else {
//                MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
//                NSString *url = [NSString stringWithFormat:@"nursing/order/appointment/list/by/%@", self.order.orderId];
//                [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
//                    NSArray *array = result[@"data"];
//                    if (array.count > 0) {
//                        self.serviceArray = array;
//                        WCNServiceRecordVC *vc = [[WCNServiceRecordVC alloc] initWithArray:array];
//                        [self.navigationController pushViewController:vc animated:YES];
//                    }
//                } failure:nil netError:nil always:^{
//                    [hud hide:YES];
//                }];
//            }
//        } else if (indexPath.row == 1) {
//            if (self.order.commentId) {
//                OrderComment *comment = [[WCCoreDataStore shareStore] getOrderCommentById:self.order.commentId];
//                if (comment) {
//                    WCNCommentVC *vc = [[WCNCommentVC alloc] init];
//                    vc.comment = comment;
//                    [self.navigationController pushViewController:vc animated:YES];
//                } else {
//                    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
//                    NSString *url = [NSString stringWithFormat:@"nursing/order/comment/detail/%@", self.order.commentId];
//                    [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
//                        NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:result[@"data"]];
//                        data[@"id"] = self.order.commentId;
//                        WCNCommentVC *vc = [[WCNCommentVC alloc] init];
//                        vc.comment = [[WCCoreDataStore shareStore] createOrderComment:data save:YES];
//                        [self.navigationController pushViewController:vc animated:YES];
//                    } failure:nil netError:nil always:^{
//                        [hud hide:YES];
//                    }];
//                }
//            } else {
//                if (self.comment) {
//                    WCNCommentVC *vc = [[WCNCommentVC alloc] init];
//                    vc.comment = self.comment;
//                    [self.navigationController pushViewController:vc animated:YES];
//                } else {
//                    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
//                    NSString *url = [NSString stringWithFormat:@"nursing/order/comment/detail/by/%@", self.order.orderId];
//                    [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
//                        self.comment = [[WCCoreDataStore shareStore] createOrderComment:result[@"data"] save:NO];
//                        
//                        WCNCommentVC *vc = [[WCNCommentVC alloc] init];
//                        vc.comment = self.comment;
//                        [self.navigationController pushViewController:vc animated:YES];
//                    } failure:nil netError:nil always:^{
//                        [hud hide:YES];
//                    }];
//                }
//            }
//        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            self.patientInfoCell.nurseName.text = [NSString stringWithFormat:@"用户:%@", self.order.patientName];
            self.patientInfoCell.nursePhone.text = self.order.patientPhone;
            return self.patientInfoCell;
        } else {
            return self.remarkCell;
        }
    } else if (indexPath.section == 1) {
        WCNOrderDetailInfoTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WCNOrderDetailInfoTCell" forIndexPath:indexPath];
        [cell configWithOrder:self.order];
    
        return cell;
    }else if (indexPath.section ==2){
        WCPicsCell *cell =[WCPicsCell tableView:tableView indexPath:indexPath];
        
        [cell addPics:_imageArray reloadTalbeView:nil];
        weakSelf_define;
        cell.getVC =^(){
        
            return weakSelf;
        };
        _cell =cell;
        return cell;
    
    }else {
        WCNSysDetailTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WCNSysDetailTCell" forIndexPath:indexPath];
        cell.textLabel.textColor = HexRGB(0x61606f);
        cell.textLabel.font = [UIFont systemFontOfSize:15.0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        if (indexPath.row == 0) {
//            cell.textLabel.text = @"服务记录";
//        } else {
            cell.textLabel.text = @"服务评价";
            if (self.order.commentId) {
                NSString *pingjia = [NSString stringWithFormat:@"%.1f", [self.order.ratingGeneral  floatValue]];
                NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName: HexRGB(0x61606f)};
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:pingjia attributes:attributes];
                NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
                textAttachment.image = [UIImage imageNamed:@"Star_show"];
                NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
                [attributedString insertAttributedString:attrStringWithImage atIndex:attributedString.length];
                cell.detailTextLabel.attributedText = attributedString;
//            }
        }
        
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                self.patientInfoCell.address.text = [NSString stringWithFormat:@"地址:%@", self.order.patientAddress];
                [self.patientInfoCell.address showAttributedText:0 lineSpace:5];
                //动态cell的高度
                CGSize size = [self.patientInfoCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
                return MAX(kOnePX+size.height, 65.0);
            }
            case 1:{
                if (self.order.patientNote) {
                    self.remarkCell.remark.text = [NSString stringWithFormat:@"症状描述:%@", self.order.patientNote];
                } else {
                    self.remarkCell.remark.text = @"症状描述:无";
                }
                [self.remarkCell.remark showAttributedText:0 lineSpace:5];
                CGSize size = [self.remarkCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
                return MAX(kOnePX+size.height, 44.0);
            }
        }
    } else if (indexPath.section == 1) {
        CGFloat h=[self.order.serviceDesc boundingRectWithSize:CGSizeMake(kScreenWidth - 100, MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height + 10;
        if ([self.order.service_Type.serviceName isEqualToString:@"预约护理门诊"]) {
            if ([self.order.service_Type.serviceType isEqualToString:@"OTHER"]) {
                return 246  -35*3;
            }else {
             return 246 + 35 -35*3;
            }
        }
        if ([self.order.service_Type.serviceType isEqualToString:@"OTHER"]) {
            return 246 ;
        }else {
            return 246 + 35;
        }
    } else if (indexPath.section ==2){
        if (_imageArray.count ==0) {
            return 0;
        }
        return 70;
    
    }else if (indexPath.section ==3){
        return 44.0;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        if ([self.order.service_Type.serviceName isEqualToString:@"预约护理门诊"]) {
            return 10 -3;
        }
        return 10;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num =0;
    if (section == 0) {
        num  =2;
    } else if (section == 1) {
         num  =1;
    }else if  (section == 2){
         num  =1;
    
    }else  if (section ==3){
         num  =1;
    }
    return num;
}

@end
